#include "menu.h"
#include "ui_menu.h"
#include <QPushButton>
#include <QMessageBox>
#include <QPixmap>
#include <QPainter>
#include <QFont>
#include <QFontDatabase>
#include "mainscene.h"
Menu::Menu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Menu),
    gameScene(nullptr),
    m_settings("MyCompany", "AircraftGame")  // 初始化QSettings
{
    ui->setupUi(this);

    m_background.load(MENU_BACKGROUND);
    setWindowTitle("飞机大战 - 主菜单");
    setFixedSize(m_background.size());

    // 设置标题样式
    ui->label->setAlignment(Qt::AlignCenter);
    ui->label->setStyleSheet(
        "font: bold 36pt 'Microsoft YaHei';"
        "color: #FFD700;"
        "text-shadow: 3px 3px 3px #000;"
    );
    ui->label->setGeometry(0, 80, width(), 100);

    // 设置按钮容器布局
    ui->verticalLayoutWidget->setGeometry(
        width()/2 - 125,  // 居中
        250,
        250,
        300
    );

    // 设置按钮公共属性
    QString buttonStyle =
        "QPushButton {"
        "   font: bold 18pt 'Microsoft YaHei';"
        "   color: white;"
        "   border-radius: 10px;"
        "   padding: 5px;"
        "   min-height: 60px;"
        "   border: 2px solid;"
        "}"
        "QPushButton:hover {"
        "   opacity: 0.9;"
        "}"
        "QPushButton:pressed {"
        "   opacity: 0.8;"
        "}";

    // 设置单个按钮样式
    ui->pushButton_2->setStyleSheet(
        buttonStyle +
        "QPushButton {"
        "   background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "                                     stop:0 #4169E1, stop:1 #1E90FF);"
        "   border-color: #1E90FF;"
        "}"
    );

    ui->pushButton_3->setStyleSheet(
        buttonStyle +
        "QPushButton {"
        "   background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "                                     stop:0 #32CD32, stop:1 #228B22);"
        "   border-color: #228B22;"
        "}"
    );

    ui->pushButton->setStyleSheet(
        buttonStyle +
        "QPushButton {"
        "   background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
        "                                     stop:0 #FF4500, stop:1 #FF6347);"
        "   border-color: #FF6347;"
        "}"
    );

    // 设置按钮间距
    ui->verticalLayout->setSpacing(20);

    // 连接按钮信号到槽函数
    connect(ui->pushButton_2, &QPushButton::clicked, this, &Menu::onSinglePlayerClicked);
    connect(ui->pushButton_3, &QPushButton::clicked, this, &Menu::onCoopModeClicked);
    connect(ui->pushButton, &QPushButton::clicked, this, &Menu::onVersusModeClicked);
}
int Menu::getHighScore(bool isSinglePlayer) const {
    QString key = isSinglePlayer ? "singlePlayerHighScore" : "coopHighScore";
    return m_settings.value(key, 0).toInt();
}
void Menu::saveHighScore(bool isSinglePlayer, int score) {
    QString key = isSinglePlayer ? "singlePlayerHighScore" : "coopHighScore";
    int currentHigh = m_settings.value(key, 0).toInt();
    if (score > currentHigh) {
        m_settings.setValue(key, score);
        m_settings.sync(); // 确保立即写入
    }
}



Menu::~Menu()
{
    delete ui;
    if(gameScene) {
        gameScene->deleteLater();
    }
}

void Menu::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.drawPixmap(0, 0, m_background);

    // 调用基类的paintEvent以确保其他元素正常绘制
    QWidget::paintEvent(event);
}

void Menu::onSinglePlayerClicked()
{
    hide();

    if(gameScene) {
        gameScene->disconnect();
        gameScene->deleteLater();
        gameScene = nullptr;
    }

    // 修改这里：不再设置父对象，使用独立的窗口
    gameScene = new MainScene();
    gameScene->setWindowFlags(Qt::Window);

    // 设置游戏模式
    gameScene->resetGame(false); // 单人模式
    //gameScene->setHighScore(getHighScore(true));

    // 连接信号
    connect(gameScene, &MainScene::returnToMenuRequested, this, [this]() {
        if(gameScene) {
            saveHighScore(true, gameScene->getPlayer1Score());
            gameScene->deleteLater();
            gameScene = nullptr;
        }
        show();
    });

    gameScene->show();
    gameScene->playGame(); // 确保调用playGame启动游戏
}

void Menu::onCoopModeClicked()
{
    hide();

    if(gameScene) {
        gameScene->disconnect();
        gameScene->deleteLater();
    }

    // 修改这里：不再设置父对象，使用独立的窗口
    gameScene = new MainScene();
    gameScene->setWindowFlags(Qt::Window);

    gameScene->resetGame(true); // 设置为合作模式
   // gameScene->setHighScore(getHighScore(false));

    connect(gameScene, &MainScene::returnToMenuRequested, this, [this]() {
        if(gameScene) {
            saveHighScore(false, gameScene->getPlayer1Score() + gameScene->getPlayer2Score());
            gameScene->deleteLater();
            gameScene = nullptr;
        }
        show();
    });

    gameScene->show();
    gameScene->playGame(); // 确保调用playGame启动游戏
}
void Menu::onVersusModeClicked()
{
    QMessageBox::information(this, "提示", "双人对抗模式正在开发中，敬请期待！");
}
